import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';

typedef ItemBuilder<ItemType> = Function(
    BuildContext context, ItemType item, int index);

class MyPagedListView<PageKeyType, ItemType> extends HookWidget {
  final PagingController<PageKeyType, ItemType> pagingController;
  final ItemBuilder<ItemType> itemBuilder;

  const MyPagedListView({
    Key? key,
    required this.pagingController,
    required this.itemBuilder,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return PagedListView(
      pagingController: pagingController,
      builderDelegate: _buildPagedChildBuilderDelegate(context, itemBuilder),
    );
  }
}

PagedChildBuilderDelegate<ItemType> _buildPagedChildBuilderDelegate<ItemType>(
    BuildContext context, ItemBuilder<ItemType> itemBuilder) {
  return PagedChildBuilderDelegate<ItemType>(
    itemBuilder: (BuildContext context, ItemType item, int index) {
      return itemBuilder(context, item, index);
    },
    noItemsFoundIndicatorBuilder: (_) => Center(
      child: SvgPicture.asset(
        "assets/icons/empty_box.svg",
        colorFilter: ColorFilter.mode(
            Theme.of(context).colorScheme.inversePrimary, BlendMode.srcIn),
        width: 200,
        height: 200,
      ),
    ),
  );
}

class MyPagedSliverList<PageKeyType, ItemType> extends HookWidget {
  final PagingController<PageKeyType, ItemType> pagingController;
  final ItemBuilder<ItemType> itemBuilder;

  const MyPagedSliverList({
    Key? key,
    required this.pagingController,
    required this.itemBuilder,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return PagedSliverList(
      pagingController: pagingController,
      builderDelegate: _buildPagedChildBuilderDelegate(context, itemBuilder),
    );
  }
}
